package com.itheima.mp.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.itheima.mp.domain.po.Address;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.vo.AddressVO;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.enums.UserStatus;
import com.itheima.mp.mapper.UserMapper;
import com.itheima.mp.service.UserServic;
import org.springframework.stereotype.Service;

import javax.management.remote.rmi.RMIServerImpl;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserServic {

    @Override
    public void deductMoney(Long id, Integer money) {
        User user = getById(id);
        if(user==null||user.getStatus()==UserStatus.FREEZE){
            throw new RuntimeException("用户已被冻结");
        }
        if(user.getBalance()<money){
            throw new RuntimeException("余额不足");
        }
        int remoney=user.getBalance()-money;
        lambdaUpdate()
                .set(User::getBalance, remoney)
                .set(remoney==0,User::getStatus,UserStatus.FREEZE)
                .eq(User::getId,id)
                .eq(User::getBalance,user.getBalance())
                .update();

    }

    @Override
    public List<User> queryUsers(String name, Integer status, Integer minBalance, Integer maxBalance) {
            return  lambdaQuery()
                .like(name != null, User::getUsername, name)
                .eq(status != null, User::getStatus, status)
                .ge(minBalance != null, User::getBalance, minBalance)
                .le(maxBalance != null, User::getBalance, maxBalance)
                .list();

    }

    @Override
    public UserVO queryUserAndAddressById(long id) {
        User user = getById(id);
        if(user==null||user.getStatus()== UserStatus.FREEZE)
        {
            throw new RuntimeException("用户已被冻结");
        }
        List<Address> list = Db.lambdaQuery(Address.class)
                .eq(Address::getUserId, id).list();
        UserVO userVO = new UserVO();
        BeanUtil.copyProperties(user,userVO);
        if(CollUtil.isNotEmpty(list))
        {
            userVO.setAddressList(BeanUtil.copyToList(list, AddressVO.class));

        }
        return userVO;
    }

    @Override
    public List<UserVO> queryUsersAndAddressByIds(List<Long> ids) {
        List<User> users = listByIds(ids);
        if(CollUtil.isEmpty(users))
        {
            return Collections.emptyList();
        }
        List<Long> userIds = users.stream().map(User::getId).collect(Collectors.toList());
        List<Address> list = Db.lambdaQuery(Address.class).in(Address::getUserId, userIds).list();
        List<AddressVO> addressVOList=BeanUtil.copyToList(list, AddressVO.class);
        Map<Long, List<AddressVO>> addressMap= new HashMap<>(0);
        if (CollUtil.isNotEmpty(addressVOList)) {
            addressMap = addressVOList.stream().collect(Collectors.groupingBy(AddressVO::getUserId));
        }


        List<UserVO> userVOList =new ArrayList<>(users.size());
        for (User user : users) {

            UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
            userVOList.add(userVO);
            userVO.setAddressList(addressMap.get(user.getId()));

        }
        return userVOList;
    }


}
